
package com.shop.cloud.mall.api.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.annotation.ApiLogin;
import com.shop.cloud.mall.api.service.DistributionOrderService;
import com.shop.cloud.mall.api.service.DistributionUserService;
import com.shop.cloud.mall.api.service.UserWithdrawRecordService;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.DistributionUser;
import com.shop.cloud.mall.common.entity.UserWithdrawRecord;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 用户提现记录
 *
 * @author
 * @date 2021-05-10 14:20:02
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/userwithdrawrecord")
@Tag(description = "userwithdrawrecord", name = "用户提现记录API")
public class UserWithdrawRecordController {

    private final UserWithdrawRecordService userWithdrawRecordService;
    private final DistributionUserService distributionUserService;
    private final DistributionOrderService distributionOrderService;

    /**
     * 用户提现记录分页列表
     * @param page 分页对象
     * @param userWithdrawRecord 用户提现记录
     * @return
     */
    @Operation(summary = "用户提现记录分页列表")
    @GetMapping("/page")
	@ApiLogin
    public R getPage(Page page, UserWithdrawRecord userWithdrawRecord) {
		userWithdrawRecord.setUserId(ThirdSessionHolder.getUserId());
        return R.ok(userWithdrawRecordService.page(page, Wrappers.query(userWithdrawRecord)));
    }

    /**
     * 用户提现记录新增
     * @param userWithdrawRecord 用户提现记录
     * @return R
     */
    @Operation(summary = "用户提现记录新增")
    @PostMapping
	@ApiLogin
    public R save(@RequestBody UserWithdrawRecord userWithdrawRecord) {
        if(userWithdrawRecord.getApplyAmount().compareTo(BigDecimal.ZERO) <= 0){
            return R.failed("金额不合法");
        }
		userWithdrawRecord.setUserId(ThirdSessionHolder.getUserId());
        //校验申请金额是否小于等于用户可提现额度
        DistributionUser distributionUser = distributionUserService.getDistributionUser(userWithdrawRecord.getUserId());
        //先获取分销员的冻结金额
        BigDecimal frozenAmount = distributionOrderService.getDistributionUserFrozenAmount(userWithdrawRecord.getUserId());
        if(distributionUser.getCommissionTotal().subtract(distributionUser.getCommissionWithdrawal()).subtract(frozenAmount)
                .compareTo(userWithdrawRecord.getApplyAmount()) == -1){
            //(累计佣金金额-已提现佣金金额-冻结金额)>=申请额度才合法，否则不能提现
            return R.failed("该用户可提现额度不够申请额度");
        }
        //分销拥金提现
        if(MallConstants.USER_WITHDRAW_TYPE_1.equals(userWithdrawRecord.getWithdrawType())){
            //用户提现收款方式为银行卡
            if(MallConstants.USER_WITHDRAW_PAYMENT_METHOD_1.equals(userWithdrawRecord.getPaymentMethod())){
                userWithdrawRecord.setStatus(MallConstants.USER_WITHDRAW_RECORD_STATUS_0);
                userWithdrawRecordService.save(userWithdrawRecord);
            }
            //用户提现收款方式为转入余额
            if(MallConstants.USER_WITHDRAW_PAYMENT_METHOD_2.equals(userWithdrawRecord.getPaymentMethod())){
                userWithdrawRecord.setStatus(MallConstants.USER_WITHDRAW_RECORD_STATUS_1);
                userWithdrawRecordService.commissiontoUserBalance(userWithdrawRecord);
            }
        }
        return R.ok();
    }

    /**
     * 用户提现记录修改
     * @param userWithdrawRecord 用户提现记录
     * @return R
     */
	@ApiLogin
    @Operation(summary = "用户提现记录修改")
    @PutMapping
    public R updateById(@RequestBody UserWithdrawRecord userWithdrawRecord) {
		userWithdrawRecord.setUserId(null);
		userWithdrawRecord.setStatus(null);
        return R.ok(userWithdrawRecordService.updateById(userWithdrawRecord));
    }

    /**
     * 用户提现记录删除
     * @param id
     * @return R
     */
    @Operation(summary = "用户提现记录删除")
    @DeleteMapping("/{id}")
	@ApiLogin
    public R removeById(@PathVariable String id) {
        return R.ok(userWithdrawRecordService.removeById(id));
    }

	/**
	 * 用户提现记录查询
	 * @param id
	 * @return R
	 */
	@Operation(summary = "用户提现记录查询")
	@GetMapping("/{id}")
	@ApiLogin
	public R getById(@PathVariable("id") String id) {
		return R.ok(userWithdrawRecordService.getById(id));
	}

}
